/*!
 * \file t_de_chunked.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"

#include "../../../../src/module/http/config.h"
#include <http/comm/http_comm.h>
#include <http/http1/http_chunk.h>
#include <http/http_core.h>
#include <frct_comm.h>
#include "frreport_comm.h"
#include "../../../comm/apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}



static void __case1(void)
{
    unsigned char *p_data = NULL;
    int i_data_length = 0;
    char *p_dest = NULL;
    unsigned long ul_dest_length = 0;
    int i_ret = 0;
    FILE *fp_file = NULL;

    fp_file = fopen("t_http1/t_http_chunk/text1.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_data_length = ftell(fp_file);

    p_data = (unsigned char *)frmalloc(i_data_length + 1);
    rewind(fp_file);
    fread(p_data,i_data_length,1,fp_file);
    fclose(fp_file);
    p_data[i_data_length] = '\0';

    i_ret = de_chunked(p_data,i_data_length, &p_dest, &ul_dest_length);

    CU_ASSERT_EQUAL(i_ret,1);


    frfree(p_data);
    frfree(p_dest);

    return;
}

static void __case2(void)
{
    unsigned char *p_data = NULL;
    int i_data_length = 0;
    char *p_dest = NULL;
    unsigned long ul_dest_length = 0;
    int i_ret = 0;
    FILE *fp_file = NULL;

    fp_file = fopen("t_http1/t_http_chunk/text2.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_data_length = ftell(fp_file);

    p_data = (unsigned char *)frmalloc(i_data_length + 1);
    rewind(fp_file);
    fread(p_data,i_data_length,1,fp_file);
    fclose(fp_file);
    p_data[i_data_length] = '\0';

    i_ret = de_chunked(p_data,i_data_length, &p_dest, &ul_dest_length);

    CU_ASSERT_EQUAL(i_ret,1);
    CU_ASSERT_EQUAL(ul_dest_length,5);
    CU_ASSERT_STRING_EQUAL(p_dest,"hello");

    frfree(p_data);
    frfree(p_dest);

    return;
}

static void __case3(void)
{
    unsigned char *p_data = NULL;
    int i_data_length = 0;
    unsigned long ul_dest_length = 0;
    int i_ret = 0;
    FILE *fp_file = NULL;

    fp_file = fopen("t_http1/t_http_chunk/text2.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_data_length = ftell(fp_file);

    p_data = (unsigned char *)frmalloc(i_data_length + 1);
    rewind(fp_file);
    fread(p_data,i_data_length,1,fp_file);
    fclose(fp_file);
    p_data[i_data_length] = '\0';

    i_ret = de_chunked(p_data,i_data_length, NULL, &ul_dest_length);

    CU_ASSERT_EQUAL(i_ret,1);
    CU_ASSERT_EQUAL(ul_dest_length,0);

    frfree(p_data);

    return;
}

static void __case4(void)
{
    unsigned char *p_data = NULL;
    int i_data_length = 0;
    char *p_dest = NULL;
    unsigned long ul_dest_length = 0;
    int i_ret = 0;
    FILE *fp_file = NULL;

    fp_file = fopen("t_http1/t_http_chunk/text3.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_data_length = ftell(fp_file);

    p_data = (unsigned char *)frmalloc(i_data_length + 1);
    rewind(fp_file);
    fread(p_data,i_data_length,1,fp_file);
    fclose(fp_file);
    p_data[i_data_length] = '\0';

    i_ret = de_chunked(p_data,i_data_length, &p_dest, &ul_dest_length);

    CU_ASSERT_EQUAL(i_ret,-1);
    CU_ASSERT_PTR_NULL(p_dest);

    frfree(p_data);
    frfree(p_dest);

    return;
}

static void __case5(void)
{
    unsigned char *p_data = NULL;
    int i_data_length = 0;
    char *p_dest = NULL;
    unsigned long ul_dest_length = 0;
    int i_ret = 0;
    FILE *fp_file = NULL;

    fp_file = fopen("t_http1/t_http_chunk/text4.txt","r");
    fseek(fp_file,0,SEEK_END);
    i_data_length = ftell(fp_file);

    p_data = (unsigned char *)frmalloc(i_data_length + 1);
    rewind(fp_file);
    fread(p_data,i_data_length,1,fp_file);
    fclose(fp_file);
    p_data[i_data_length] = '\0';

    i_ret = de_chunked(p_data,i_data_length, &p_dest, &ul_dest_length);

    CU_ASSERT_EQUAL(i_ret,0);
    CU_ASSERT_PTR_NULL(p_dest);

    frfree(p_data);
    frfree(p_dest);

    return;
}

int add_suite_de_chunked(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" de_chunked()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);

    return FR_SUCC;
}
